/**
 * VideoWindow : For Linux and Windows video
 *
 * Created : Nov 26, 2012
 *
 * @author pquiring
 */

import java.awt.*;
import java.util.*;

import javaforce.*;
import javaforce.voip.*;

public class VideoWindow extends javax.swing.JFrame {

  /**
   * Creates new form VideoWindow
   */
  public VideoWindow(VideoController vc, int line) {
    initComponents();
    setLayout(new VideoLayout());
    setVisible(true);
    this.vc = vc;
    this.line = line;
    localCamera = new User();
    add(localCamera.canvas);
    doLayout();
  }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    setTitle("TeleVideo");
    setName("JPLCamera"); // NOI18N
    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        formWindowClosing(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 609, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 375, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    vc.toggleVideoPanel(line);
  }//GEN-LAST:event_formWindowClosing

  // Variables declaration - do not modify//GEN-BEGIN:variables
  // End of variables declaration//GEN-END:variables

  private VideoController vc;

  private static class User {
    public RTPChannel channel;
    public JFImage img;
    public Canvas canvas = new Canvas() {
      public void paint(Graphics g) {
        if (img == null) return;
        g.drawImage(img.getImage(), 0, 0, null);
      }
      public void update(Graphics g) {
        paint(g);
      }
    };
  }

  private ArrayList<User> users = new ArrayList<User>();
  private final Object lock = new Object();
  private int line;
  private User localCamera;

  public void setRemoteImage(RTPChannel channel, JFImage img) {
    //find canvas for channel
    User user = findUser(channel);
    if (user == null) return;
    user.img = img;
    user.canvas.repaint();
  }

  public void setLocalImage(JFImage img) {
    localCamera.img = img;
    localCamera.canvas.repaint();
  }

  public void resize() {
    EventQueue.invokeLater(new Runnable(){
      public void run() {
        VideoWindow.this.revalidate();
        doLayout();
      }
    });
  }

  private User findUser(RTPChannel channel) {
    synchronized(lock) {
      for(int a=0;a<users.size();a++) {
        User user = users.get(a);
        if (user.channel == channel) return user;
      }
    }
    return null;
  }

  public void addCamera(RTPChannel channel) {
    User user = new User();
    user.channel = channel;
    synchronized(lock) {
      users.add(user);
    }
    add(user.canvas);
    resize();
  }

  public void delCamera(RTPChannel channel) {
    synchronized(lock) {
      for(int a=0;a<users.size();a++) {
        User user = users.get(a);
        if (user.channel == channel) {
          users.remove(user);
          remove(user.canvas);
          resize();
          return;
        }
      }
    }
  }

  private static int X = 352;
  private static int Y = 288;

  private int wx = 0, wy = 0;

  public class VideoLayout implements LayoutManager {
    public void addLayoutComponent(String string, Component cmpnt) {
    }

    public void removeLayoutComponent(Component cmpnt) {
    }

    public Dimension preferredLayoutSize(Container cntnr) {
      return null;
    }

    public Dimension minimumLayoutSize(Container cntnr) {
      return null;
    }

    public void layoutContainer(Container cont) {
      Insets insets = VideoWindow.this.getInsets();
      Component cs[] = cont.getComponents();
      int x = 0;
      int y = 0;
      int mx = 0;
      int my = 0;
      Rectangle s = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
      int w = s.width;
      int h = s.height;
      for(int a=0;a<cs.length;a++) {
        Component c = cs[a];
        c.setBounds(x,y,X,Y);
        x += X;
        if (x > mx) mx = x;
        if (x + Y > w) {
          x = 0;
          y += Y;
        }
      }
      if (x == 0 && cs.length > 0) y -= Y;
      my = y + Y;
      if (wx != mx || wy != my) {
        wx = mx;
        wy = my;
        setSize(insets.left + insets.right + mx, insets.top + insets.bottom + my);
      }
    }
  }
}
